<?php
/**
 * Memcached/memcachedb
 *
 */

class ApiMemcache {

	private $_servers;
	private $_memcache = null;

	private function __construct($config = array())
	{
		$this->_servers = $config['servers'];
		$this->_memcache = new Memcache;

		foreach($this->_servers as $v)
		{
			$this->_memcache->addServer($v['host'], $v['port'], $v['weight']);
		}
		//$this->_memcache->setCompressThreshold(20000, 0.2);
	}

    public static function getInstance ($config = array())
    {
		static $instances = array();

		$index 	= serialize($config);
		if(isset($instances[$index]))
			return $instances[$index];

		$class 	= __CLASS__;
		$obj	= new $class($config);
		$instances[$index] = $obj;

		return $obj;
    }

	public function set($key, $value, $expire = 0) {
		return $this->_memcache->set($key, $value, FALSE, $expire);
	}

	public function delete($key){
		return $this->_memcache->delete($key);
	}

	public function get($key){
		return $this->_memcache->get($key);
	}

	public function replace($key, $value, $expire = 0) {
		return $this->_memcache->replace($key, $value, FALSE, $expire);
	}
}
